package cn.myfirefly.moming.common.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.DataAuthObjectDao;
import cn.myfirefly.moming.common.entity.MmDataAuthObject;
import cn.myfirefly.moming.common.model.DataAuthObjectDto;
import cn.myfirefly.moming.common.model.DataAuthObjectFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;

@Service
@Transactional
public class DataAuthObjectService {

	@Autowired
	private DataAuthObjectDao dataAuthObjectDao;
	
	@SuppressWarnings("rawtypes")
	public PageResultDto getList(DataAuthObjectFilterDto dto) {
		
		return dataAuthObjectDao.getList(dto);
	}
	
	public List<MmDataAuthObject> getList() {
		
		return dataAuthObjectDao.getList();
	}

	public DataAuthObjectDto getDataAuthObject(Integer id) {
		MmDataAuthObject mmDataAuthObject = dataAuthObjectDao.get(id);
		DataAuthObjectDto dataAuthObjectDto = BeanUtil.copyBeans(DataAuthObjectDto.class, mmDataAuthObject);
		return dataAuthObjectDto;
	}
	
	public String del(Integer id) {
		MmDataAuthObject mmDataAuthObject = dataAuthObjectDao.get(id);
		if (mmDataAuthObject == null) {
			return "该信息不存在，请尝试刷新列表后再操作。";
		}
		dataAuthObjectDao.del(mmDataAuthObject);
		return Const.ResultCode.SUCCESS;
	}
	
	public Integer insert(DataAuthObjectDto dto) {
		MmDataAuthObject mmDataAuthObject = BeanUtil.copyBeans(MmDataAuthObject.class, dto);
		return dataAuthObjectDao.save(mmDataAuthObject);
	}
	
	public Integer update(DataAuthObjectDto dto) {
		MmDataAuthObject mmDataAuthObject = dataAuthObjectDao.get(dto.getId());
		if (mmDataAuthObject == null) {
			return -1;
		}
		BeanUtil.copyBeans(dto, mmDataAuthObject);
		return dataAuthObjectDao.save(mmDataAuthObject);
	}
}
